package com.m4f.city.web.controller.manager;

import java.security.Principal;
import java.util.Locale;
import java.util.logging.Logger;

import javax.cache.CacheException;
import javax.validation.Valid;

import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.m4f.city.business.domain.InternalUser;
import com.m4f.city.business.domain.Organization;
import com.m4f.city.business.security.InternalUserDetailService;
import com.m4f.city.web.controller.BaseController;
import com.m4f.city.web.form.OrganizationForm;
import com.m4f.gaeweb.web.utils.StackTraceUtil;

@Controller
@Secured(InternalUserDetailService.ROLE_MANAGER)
@RequestMapping("/manager/organization")
public class OrganizationManagerController extends BaseController {
	private static final Logger LOGGER = Logger.getLogger(OrganizationManagerController.class.getName());
	
	public OrganizationManagerController() throws CacheException {
		super();
	}
	
	@RequestMapping(method=RequestMethod.GET)
	public String edit(Model model, Principal currentUser, Locale locale) {
		try {
			InternalUser user = getInternalUser(currentUser);
			Organization org = this.serviceLocator.getOrganizationService().getOrganization(user.getOrganizationId());
			
			model.addAttribute("form", new OrganizationForm(org));
		} catch(Exception e) {
			LOGGER.severe(StackTraceUtil.getStackTrace(e));
			return "common.error";
		}
		return "manager.organization.edit";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public String save(@ModelAttribute("form") @Valid OrganizationForm form, 
			BindingResult result, Model model, Locale locale) {
		// Check if exist an user with this mail
		boolean updateAction = false;
		try {
			if(result.hasErrors()) {
				return "manager.organization.edit";
			}
			
			Organization org = form.getOrganization();
			if(org.getId() != null) updateAction = true;
			this.serviceLocator.getOrganizationService().save(org);
			if(updateAction) addSuccessMessage("message.success.admin.organization.updated");
			else addSuccessMessage("message.success.admin.organization.created");
		} catch(Exception e) {
			if(updateAction) addErrorMessage("message.error.admin.organization.updated");
			else addErrorMessage("message.error.admin.organization.created");
			LOGGER.severe(StackTraceUtil.getStackTrace(e));
			return "common.error";
		}
		
		return getRedirect("/manager/organization", locale);
	}
	
	@RequestMapping(value="/detail/{orgId}", method=RequestMethod.GET)
	public String detail(@PathVariable Long orgId, Model model, Locale locale) {
		try {
			Organization org = this.serviceLocator.getOrganizationService().getOrganization(orgId);
			model.addAttribute("organization", org);
		} catch(Exception e) {
			LOGGER.severe(StackTraceUtil.getStackTrace(e));
			return "common.error";
		}
		return "manager.organization.detail";
	}
}